package com.wei.czz.framework.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.store.dao.PreStoreDao;
import com.wei.czz.framework.store.entity.PreStoreEntity;
import com.wei.czz.framework.store.service.PreStoreService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;


@Service("preStoreService")
@AllArgsConstructor
public class PreStoreServiceImpl extends ServiceImpl<PreStoreDao, PreStoreEntity> implements PreStoreService {

    private static final Logger log = LoggerFactory.getLogger(PreStoreServiceImpl.class);

    @Override
    public void savePreStore(Long businessId, Long storeId) {
        UserPo userPo = SecurityUtils.getUser();

        LambdaQueryWrapper<PreStoreEntity> preStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        preStoreLambdaQueryWrapper.eq(PreStoreEntity::getUserId, userPo.getUserId())
                .eq(PreStoreEntity::getBusinessId, businessId);
        // 查询
        List<PreStoreEntity> preStoreList = baseMapper.selectList(preStoreLambdaQueryWrapper);
        if (preStoreList.size() > 1) {
            log.error("用户商店前置存在多条记录。businessId={} storeId={}", businessId, storeId);
        }
        if (preStoreList.isEmpty()) {
            // 不存在记录
            PreStoreEntity preStore = this.newPreStore(businessId, storeId);
            Integer num = baseMapper.insert(preStore);
            log.info("添加用户商店前置完成。count={}", num);
        } else {
            PreStoreEntity preStore = preStoreList.get(0);
            if (preStore.getStoreId().equals(storeId)) {
                log.info("用户商店前置内容一致。_storeId={} storeId={}", preStore.getStoreId(), storeId);
                return;
            }

            PreStoreEntity newPreStore = new PreStoreEntity();
            newPreStore.setId(preStore.getId())
                    .setStoreId(storeId)
                    .setUpdateTime(new Date())
                    .setUpdateUser(userPo.getUsername())
                    .setUpdateUserId(userPo.getUserId());
            // 更新
            int count = baseMapper.updateById(newPreStore);
            log.info("更新用户商店前置完成。count={}", count);
        }
    }

    @Override
    public void batchDeletePreStore(Long userId, List<Long> storeIdList) {
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<PreStoreEntity> preStoreLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        preStoreLambdaUpdateWrapper.set(PreStoreEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                        .set(PreStoreEntity::getUpdateTime, date)
                        .set(PreStoreEntity::getUpdateUser, userPo.getUsername())
                        .set(PreStoreEntity::getUpdateUserId, userPo.getUserId());
        preStoreLambdaUpdateWrapper.eq(PreStoreEntity::getUserId, userId)
                .in(PreStoreEntity::getStoreId, storeIdList)
                .eq(PreStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(null, preStoreLambdaUpdateWrapper);
        log.info("删除用户商店前置完成。count={}", count);
    }

    private PreStoreEntity newPreStore(Long businessId, Long storeId) {
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        PreStoreEntity preStore = new PreStoreEntity();
        preStore.setUserId(userPo.getUserId())
                .setBusinessId(businessId)
                .setStoreId(storeId)
                .setDeleteStatus(CommonEnum.ZERO.getValue())
                .setUpdateTime(date)
                .setUpdateUser(StringUtils.EMPTY)
                .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                .setCreateTime(date)
                .setCreateUser(userPo.getUsername())
                .setCreateUserId(userPo.getUserId());
        return preStore;
    }
}